﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Profile;
using System.Data;
using Gift.Model.ENUM;
using Gift.DAL;
using Gift.Model;

namespace WebSite.admin.product
{
    public partial class category_add : BaseAdmin
    {
        readonly DALGoodsType theGoodsType = new DALGoodsType();
        tbGoodsType oGoodsType;

        bool UploadPhoto()
        {
            if (this.fileTypePhoto.FileName.Contains("\\"))
            {
                string fileName = this.fileTypePhoto.ShortFileName;
                fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
                fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
                if (this.fileTypePhoto.HasFile)
                {
                    this.fileTypePhoto.SaveAs(Server.MapPath("~/uploadfile/product/" + fileName));
                }
                this.lblPhotoUrl.Text = fileName;
                return true;
            }
            else
            {
                ExtAspNet.Alert.Show("请选择上传的图片！");
            }
            return false;
        }

        private void Display(string parentid, string space)
        {
            string sql = "Select * From tbGoodsType Where parentId =" + parentid + " and isnull(visible,0)=1 and typedepth<=1 Order By typeOrder";
            DataTable table = new DataTable();
            table = SQLAccess.GreatDs(sql).Tables[0];
            if (table.Rows.Count > 0)
            {
                foreach (DataRow row in table.Rows)
                {
                    this.ddlGoodsType.Items.Add(new ExtAspNet.ListItem(space + "" + row["typeName"].ToString() + "", row["typeId"].ToString().Trim()));
                    this.Display(row["typeId"].ToString(), "|-" + space);
                }
            }
            if (this.ddlGoodsType.Items.Count <=0 || ddlGoodsType.Items[0].Value != "0")
            {
                this.ddlGoodsType.Items.Insert(0, new ExtAspNet.ListItem("顶级分类", "0"));
                this.ddlGoodsType.SelectedValue = this.ddlGoodsType.Items[0].Value;
            }
        }



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.ValidateLoginRule(ERule.AddProductClass);
                this.Display("0", "|--");
                if (base.Request.QueryString["cid"] != null)
                {
                    this.upbind();
                }
            }
        }

        private void upbind()
        {
            oGoodsType = theGoodsType.GetModel(int.Parse(Request.QueryString["cid"]));
            if(oGoodsType != null)
            {
                if (this.ddlGoodsType.Items.FindByValue(oGoodsType.parentId.ToString()) != null)
                {
                    this.ddlGoodsType.SelectedValue = this.ddlGoodsType.Items.FindByValue(oGoodsType.parentId.ToString()).Value;
                }
                this.txtTypeName.Text = oGoodsType.typeName;
                this.txtOrder.Text = oGoodsType.typeOrder.ToString();
                this.isVisible.Checked = oGoodsType.visible;
                this.txtTypeLink.Text = oGoodsType.typeLink;
                if (oGoodsType.typePic != null)
                {
                    this.lblPhotoUrl.Text = oGoodsType.typePic.ToString();
                    this.fileTypePhoto.Required = false;
                    this.fileTypePhoto.ShowRedStar = false;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        { 
                if (base.Request.QueryString["cid"] != null)
                {
                    if (this.fileTypePhoto.FileName.Trim().Length > 0)
                    {
                        this.UploadPhoto();
                    }
                    oGoodsType = theGoodsType.GetModel(int.Parse(Request.QueryString["cid"]));
                    oGoodsType.typeName = this.txtTypeName.Text.Trim();
                    oGoodsType.typeOrder = int.Parse(this.txtOrder.Text);
                    oGoodsType.parentId = int.Parse(this.ddlGoodsType.SelectedItem.Value);
                    if (oGoodsType.parentId == 0)
                    {
                        oGoodsType.typedepth = 0;
                    }
                    else
                    {
                        tbGoodsType tempType = theGoodsType.GetModel(oGoodsType.parentId);
                        oGoodsType.typedepth = tempType.typedepth + 1;
                    }
                    oGoodsType.typePic = this.lblPhotoUrl.Text;
                    oGoodsType.visible = this.isVisible.Checked;
                    oGoodsType.typeLink = this.txtTypeLink.Text.Trim();
                    theGoodsType.Update(oGoodsType);

                    ExtAspNet.Alert.Show("商品类别修改成功！", "", "openNewTab('ProductClass','product/category.aspx','分类列表','AddProductClass')");
                }
                else
                {
                    this.UploadPhoto();
                    oGoodsType = new tbGoodsType();
                    oGoodsType.typeName = this.txtTypeName.Text.Trim();
                    oGoodsType.typeOrder = int.Parse(this.txtOrder.Text);
                    oGoodsType.parentId = int.Parse(this.ddlGoodsType.SelectedItem.Value);
                    oGoodsType.parentId = int.Parse(this.ddlGoodsType.SelectedItem.Value);
                    if (oGoodsType.parentId == 0)
                    {
                        oGoodsType.typedepth = 0;
                    }
                    else
                    {
                        tbGoodsType tempType = theGoodsType.GetModel(oGoodsType.parentId);
                        oGoodsType.typedepth = tempType.typedepth + 1;
                    }
                    oGoodsType.typePic = this.lblPhotoUrl.Text;
                    oGoodsType.visible = this.isVisible.Checked;
                    oGoodsType.typeLink = this.txtTypeLink.Text.Trim();
                    theGoodsType.Add(oGoodsType);
                    ExtAspNet.Alert.Show("商品类别添加成功！", "", "openNewTab('ProductClass','product/category.aspx','分类列表','AddProductClass')");

                }
            
        }


    }
}